package com.chain.qs.modules.biz.web;

import com.chain.qs.common.web.BaseController;
import com.chain.qs.modules.biz.entity.EventTemplate;
import com.chain.qs.modules.biz.service.EquipmentService;
import com.chain.qs.modules.biz.service.EventTemplateService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping(value = "${adminPath}/biz/et")
public class EventTemplateController extends BaseController {

    @Resource
    private EventTemplateService eventTemplateService;

    @Resource
    private EquipmentService equipmentService;

    @ModelAttribute
    public EventTemplate get(String id) {
        EventTemplate eventTemplate;
        if (id != null && !id.equals("")) {
            eventTemplate = eventTemplateService.get(id);
        } else {
            eventTemplate = new EventTemplate();
        }
        return eventTemplate;
    }

    @RequestMapping(value = "")
    public String list(Model model) {
        model.addAttribute("list", eventTemplateService.findList(null));
        return "modules/biz/eventTemplateList";
    }

    @RequestMapping(value = "form")
    public String form(Model model) {
        model.addAttribute("equList", equipmentService.findList(null));
        return "modules/biz/eventTemplateForm";
    }

    @ResponseBody
    @RequestMapping(value = "save")
    public String save(EventTemplate eventTemplate) {
        if (eventTemplateService.getByKey(eventTemplate.getKey()) != null) {
            return "error";
        } else {
            eventTemplateService.save(eventTemplate);
            return "success";
        }
    }

    @RequestMapping(value = "delete")
    public String delete(EventTemplate eventTemplate) {
        eventTemplateService.delete(eventTemplate);
        return "redirect:" + adminPath + "/biz/et";
    }
}
